class ItinerariesController < ApplicationController


  # GET /itineraries
  # GET /itineraries.xml
  def index
    @itineraries = Itinerary.all
	#@itineraries = Itinerary.find(params[:id])
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml => @itineraries }
    end
  end

  # GET /itineraries/1
  # GET /itineraries/1.xml
  def show
	@title = 'Detalles de Itinerario'
	@body_tag= 'onload="initMapItinerary()" id="public"'
    @itinerary = Itinerary.find(params[:id])
	@trip = Trip.find_by_id(@itinerary.trip_id)
	@comments = Itinerary.find(params[:id]).comment
	@countVotes = Itinerary.find(params[:id]).vote
	@user= User.find(current_user.id)
	@votes = Vote.find(:all, :conditions => {:itinerary_id => params[:id], :user_id => current_user.id})
	respond_to do |format|
      format.html # show.html.erb
      format.xml  { render :xml => @itinerary }
    end
  end

  # GET /itineraries/new
  # GET /itineraries/new.xml
  def new
	@title = 'Nuevo Itinerario'
    @itinerary = Itinerary.new
	@itinerary.trip_id = params[:trip_id]
	@itinerary.iti_image_url = params[:flickrImage]
	@itinerary.iti_location = params[:iti_location]
	@itinerary.iti_latitude = params[:latitude]
	@itinerary.iti_longitude = params[:longitude]
	@trip = Trip.find(@itinerary.trip_id)
    respond_to do |format|
      format.html # new.html.erb
      format.xml  { render :xml => @itinerary }
    end
  end

  # GET /itineraries/1/edit
  def edit
    @itinerary = Itinerary.find(params[:id])
  end

  # POST /itineraries
  # POST /itineraries.xml
  def create
    @itinerary = Itinerary.new(params[:itinerary])

    respond_to do |format|
      if @itinerary.save
        flash[:notice] = 'Itinerary was successfully created.'
        ##format.html { redirect_to(@itinerary) }
		format.html { redirect_to :controller => "trips", :action => "show", :id => @itinerary.trip_id }
        format.xml  { render :xml => @itinerary, :status => :created, :location => @itinerary }
      else
        format.html { render :action => "new" }
        format.xml  { render :xml => @itinerary.errors, :status => :unprocessable_entity }
      end
    end
  end

  # PUT /itineraries/1
  # PUT /itineraries/1.xml
  def update
    @itinerary = Itinerary.find(params[:id])

    respond_to do |format|
      if @itinerary.update_attributes(params[:itinerary])
        flash[:notice] = 'Itinerary was successfully updated.'
        format.html { redirect_to(@itinerary) }
        format.xml  { head :ok }
      else
        format.html { render :action => "edit" }
        format.xml  { render :xml => @itinerary.errors, :status => :unprocessable_entity }
      end
    end
  end

  # DELETE /itineraries/1
  # DELETE /itineraries/1.xml
  def destroy
    @itinerary = Itinerary.find(params[:id])
	@trip = @itinerary.trip_id
    @itinerary.destroy

    respond_to do |format|
      format.html { redirect_to :controller => 'trips', :action => 'show', :id => @trip }
      format.xml  { head :ok }
    end
  end	 
end
